<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>可出库借货订单</title>
    <script type="text/javascript">
      var newDeliveryFromLoanWin;
      Ext.onReady(function(){

        var orderNumber = Ext.create('Ext.form.field.Text', {
          fieldLabel: '单号',
          name: 'order_number',
          anchor: '100%'
        });

        var customer = Ext.create('Ext.form.ComboBox', {
          fieldLabel: '客户',
          store: customerStore,
          valueField: 'id',
          displayField: 'name',
          name: 'customer_id',
          loadingText: '搜索中...',
          pageSize: 10,          
          minChars: 0,
          anchor: '100%',
          listConfig: {
            getInnerTpl: function() {
              return '<div data-qtip="{number}">{name}</div>';
            }
          }
        });

        var beginDate = Ext.create('Ext.form.field.Date', {
          fieldLabel: '日期',
          name: 'begin_date',
          id: 'begin_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",          
          anchor: '100%',
          vtype: 'daterange',
          endDateField: 'end_date'
        });
        
        var endDate = Ext.create('Ext.form.field.Date', {
          fieldLabel: '至',
          name: 'end_date',
          id: 'end_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",          
          anchor: '100%',
          vtype: 'daterange',
          startDateField: 'begin_date'
        });

        var submitButton = Ext.create('Ext.Button', {
          text: '搜索',
          iconCls: 'finding',
          handler: function(){
            loanDeliveryDS.removeAll();
            loanDeliveryDS.getProxy().extraParams = queryFormPanel.getForm().getFieldValues();
            toolBar.moveFirst(); 
          }
        });

        var clearButton = Ext.create('Ext.Button', {
          text: '清空',
          width:50,
          handler: function(){
            queryFormPanel.form.reset();
          }
        });

        var queryFormPanel = Ext.create('Ext.form.Panel', {
          fieldDefaults: {
            labelWidth: 65,
            labelAlign: 'right'
          },
          width: screenWidth-165,
          frame : true,
          border:false,
          items: [{
              xtype: 'fieldset',
              defaults: {width: 1000},
              title: '查询条件',
              border: true,
              items: [{
                  layout: 'column',
                  xtype: 'container',
                  items: [{
                      columnWidth: .2,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [orderNumber]
                    }, {
                      columnWidth: .38,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [customer]
                    }, {
                      columnWidth: .15,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [submitButton]
                    }, {
                      columnWidth: .15,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [clearButton]
                    }]
                },{
                  layout: 'column',
                  xtype: 'container',
                  items: [{
                      columnWidth: .2,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [beginDate]
                    }, {
                      columnWidth: .2,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [endDate]
                    }]
                }]
            }]
        });
        //被选择的订单号。
        var loanId;

        var  loanDeliveryDS = Ext.create('Ext.data.Store', {
          //autoDestroy : true,
          model : 'LoanOrderModel',
          proxy : {
            type : 'ajax',
            url : '/loan/getAllLoansForDelivery',
            reader : {
              type : 'json',
              root : 'root',
              totalProperty : 'totalProperty'// 数据集记录总数
            }
          },
          pageSize : pageSize,
          autoLoad : true
        });


        var toolBar = Ext.create('Ext.toolbar.Paging', {
          pageSize: pageSize,
          store: loanDeliveryDS,
          displayInfo: true,
          displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
          emptyMsg: "没有记录"
        })
        
        //生成要订单的产品的Grid
        var loanDeliveryGrid = Ext.create('Ext.grid.Panel', {
          title:'可出库借货订单列表',
          store: loanDeliveryDS,
          columns : [Ext.create('Ext.grid.RowNumberer'),{
              header: 'DB',
              dataIndex: 'id',
              width: 50
            },{
              header: '借货订单单号',
              dataIndex: 'order_number',
              width: 170
            },{
              header: '客户',
              dataIndex: 'customer_name',
              width: 180
            },{
              header: '要求发货日期',
              dataIndex: 'action_date',
              renderer: Ext.util.Format.dateRenderer('Y-m-d'),
              width: 85
            },{
              header: '操作员',
              dataIndex: 'employee_name',
              width: 60
            },{
              header: '部门',
              dataIndex: 'department_name',
              width: 75
            },{
              header: '审批状态',
              dataIndex: 'auditing_status',
              width: 70,
              renderer: auditingStatusRender
            },{
              header: '审批者',
              dataIndex: 'auditor_name',
              width: 60
            },{
              header: '审批意见',
              dataIndex: 'audit_opinion',
              hidden:true,
              width: 160
            },{
              header: '处理状态',
              dataIndex: 'deal_with_status',
              renderer: dealWithStatusRender,
              width: 70
            },{
              header: '出库状态',
              dataIndex: 'store_status',
              renderer: dealWithStatusRender,
              width: 70
            },{
              header: '仓库操作员',
              dataIndex: 'delivery_employee_name',
              width: 80
            },{
              header: '生成日期',
              dataIndex: 'created_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
              width: 115
            },{
              header: '修改日期',
              dataIndex: 'updated_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
              width: 115
            }],
          selModel :Ext.create('Ext.selection.CheckboxModel'),
          loadMask: true,
          width: screenWidth-165,
          height: 315,
          frame: true,
          bbar: toolBar    
        });

        loanDeliveryGrid.on('itemcontextmenu', function(grid, record,item,index,e,options){
          loanId = loanDeliveryDS.getAt(index).get('id');
          var dealWithStatus = loanDeliveryDS.getAt(index).get('deal_with_status');
          var storeStatus = loanDeliveryDS.getAt(index).get('store_status');
          loanNumber = loanDeliveryDS.getAt(index).get('order_number');
          transformToDelivery.setDisabled(true);
          if(dealWithStatus != "已经出货" && dealWithStatus != "撤消" && dealWithStatus != "暂存新增"){
            transformToDelivery.setDisabled(false);
          } 
          if(storeStatus == '出库完毕'){
            transformToDelivery.setDisabled(true);
          }
          e.preventDefault();
          rightClick.showAt(e.getXY());
        });

        var transformToDelivery = Ext.create('Ext.menu.Item', {
          text: '【转为借货出库单】',
          //iconCls: 'album',
          handler: rightMenuTransformToLoanDeliveryFn
        });
        
        function rightMenuTransformToLoanDeliveryFn(){
          newDeliveryFromLoanWin = Ext.create('Ext.window.Window', {
            layout: 'fit',
            width: 967,
            height: 510,
            closeAction: 'destroy',
            plain: true,
            modal: true,
            constrain:true,
            //modal: true,
            title: '借货订单:'+loanNumber+'转换为出库单',
            autoLoad: {
              url: "/loan_delivery/newDeliveryFromLoan?loan_id="+loanId,
              scripts: true
            }
          });
          newDeliveryFromLoanWin.on("destroy",function(){
            loanDeliveryDS.load();
          });
          newDeliveryFromLoanWin.resizable = false;
          newDeliveryFromLoanWin.show();
        }
 
        var rightClick = Ext.create('Ext.menu.Menu', {
          items: [transformToDelivery]
        });

        loanDeliveryGrid.on('itemdblclick', function(gridPanel, record,item,index,e,options){
          loanId = loanDeliveryDS.getAt(index).get('id');
          loanNumber = loanDeliveryDS.getAt(index).get('order_number');
          //loanDeliveryDS.load();
          loanDetailDS.removeAll();
          loanDetailDS.load({params:{'loan_id': loanId}});
          loanDetailGrid.setTitle("借货订单："+loanNumber+"，的详细信息");
        });

        //--------------------------借货订单详细，双击后可显示数据----------------
        //借货订单Grid Store Detail
        
        var loanDetailDS = Ext.create('Ext.data.Store', {
          //autoDestroy : true,
          model : 'LoanDeliveryDetailModel',
          proxy : {
            type : 'ajax',
            url : '/loan/getLoanDetailsById',
            reader : {
              type : 'json',
              root : 'root',
              totalProperty : 'totalProperty'// 数据集记录总数
            }
          }
        });

        //生成要订单的产品的Grid
        var loanDetailGrid = Ext.create('Ext.grid.Panel', {
          title:'订单详细列表',
          store: loanDetailDS,
          columns : [Ext.create('Ext.grid.RowNumberer'),{
              header: 'DB',
              dataIndex: 'id',
              width: 60
            },{
              header: '产品名称',
              dataIndex: 'product_name',
              width: 270
            }, {
              header: '产品编号',
              dataIndex: 'product_number',
              width:95
            },{
              header: '规格',
              dataIndex: 'product_spec',
              width: 70
            },{
              header: '数量',
              dataIndex: 'quantity',
              align: 'right',
              width: 60
            },{
              header: '套装或单品',
              dataIndex: 'is_suit',
              width: 70
            },{
              header: '备注',
              dataIndex: 'description',
              width: 150
            }],
          loadMask: true,
          width: screenWidth-165,
          height: dgHeight,
          frame: true
        });


        //总的panel
        var workbenchPanel = Ext.create('Ext.form.Panel', {
          width: screenWidth-165,
          height: totalHeight,autoScroll:true,
          frame:false,          
          layout: {type: 'table',columns:1},
          items:[queryFormPanel,loanDeliveryGrid,loanDetailGrid]
        });
        
        workbenchPanel.render("loan_delivery_list_workbench");
      })
    </script>
  </head>
  <body>
    <div id="loan_delivery_list_workbench"></div>
  </body>
</html>
